package com.geeklan.smart.ui.feed.fragment;

import android.view.View;
import android.widget.Toast;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.geeklan.base.base.BaseFragment;
import com.geeklan.smart.R;
import com.geeklan.smart.data.model.FeedRule;
import com.geeklan.smart.data.model.UpdateFeedRuleByDate;
import com.geeklan.smart.data.repository.pet.PetRepository;
import com.geeklan.smart.databinding.FragmentFeedingPlanBinding;
import com.geeklan.smart.ui.dialog.FoodBottomSheetDialog;
import com.geeklan.smart.ui.dialog.SnacksBottomSheetDialog;
import com.geeklan.smart.ui.feed.fragment.adapter.FeedingPlanAdapter;
import com.geeklan.smart.ui.viewmodel.pet.PetViewModel;
import com.geeklan.smart.ui.viewmodel.pet.PetViewModelFactory;
import com.geeklan.smart.utils.DateUtils;
import com.geeklan.smart.widget.ItemOnClickListener;

import java.util.ArrayList;
import java.util.List;

public class FeedingPlanFragment extends BaseFragment<FragmentFeedingPlanBinding> implements FeedingPlanAdapter.ItemOnClickListener {
    private PetViewModel petViewModel;
    private String deviceId;
    private List<DateUtils.DateAndWeek> datesAndWeeks;
    private List<FeedRule> timedPlans = new ArrayList<>();
    private FeedingPlanAdapter mAdapter;
    private int week = 0;
    private String date;

    public void setDatesAndWeeks(List<DateUtils.DateAndWeek> datesAndWeeks) {
        this.datesAndWeeks = datesAndWeeks;
    }

    public FeedingPlanFragment() {

    }

    @Override
    protected FragmentFeedingPlanBinding initBinding() {
        return FragmentFeedingPlanBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        deviceId = getActivity().getIntent().getStringExtra("deviceId");
        petViewModel = new ViewModelProvider(this, new PetViewModelFactory(new PetRepository())).get(PetViewModel.class);

        petViewModel.getFeedRuleList().observe(this, response -> {
            timedPlans.clear();
            timedPlans.addAll(response);
            mAdapter.notifyDataSetChanged();
            mBinding.smartRefreshLayout.finishRefresh();

            if(timedPlans.isEmpty()){
                mBinding.llNotData.setVisibility(View.VISIBLE);
                mBinding.rvFeedPlan.setVisibility(View.GONE);
            }else {
                mBinding.llNotData.setVisibility(View.GONE);
                mBinding.rvFeedPlan.setVisibility(View.VISIBLE);
            }
        });

        petViewModel.getFeedRuleErrorList().observe(this, s -> {
            Toast.makeText(mContext, s, Toast.LENGTH_SHORT).show();
            mBinding.smartRefreshLayout.finishRefresh();
        });

        mBinding.smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            for (DateUtils.DateAndWeek datesAndWeek : datesAndWeeks) {
                if (datesAndWeek.getWeek() == week) {
                    petViewModel.getFeedRuleList(deviceId, datesAndWeek.getWeek(), DateUtils.dateFormatDay1(datesAndWeek.getDay()));
                }
            }
        });
        mBinding.smartRefreshLayout.autoRefresh();


        petViewModel.getUpdateFeedPlanLiveData().observe(this, response -> {
            dismiss();
            Toast.makeText(mContext, R.string.update_success, Toast.LENGTH_SHORT).show();
//            mBinding.smartRefreshLayout.autoRefresh();
        });

        petViewModel.getUpdateFeedPlanErrorLiveData().observe(this, s -> {
            dismiss();
            Toast.makeText(mContext, s, Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    protected void initView() {
        date = DateUtils.dateFormatDay1(datesAndWeeks.get(0).getDay());
        week = datesAndWeeks.get(0).getWeek();
        mBinding.tvProgress.setText(DateUtils.dateFormatDay3(datesAndWeeks.get(0).getDay()) + "计划进度");
        mAdapter = new FeedingPlanAdapter(mContext, timedPlans);
        mAdapter.setItemOnClickListener(this);
        mBinding.rvFeedPlan.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rvFeedPlan.setAdapter(mAdapter);
        mBinding.llNext.setOnClickListener(v -> {
            SnacksBottomSheetDialog snacksBottomSheetDialog = new SnacksBottomSheetDialog(mContext);
            snacksBottomSheetDialog.setConfirmButtonClickListener((typePosition, numberPosition, time) -> {
                FeedRule addFeedRule = new FeedRule();
                addFeedRule.setDeviceId(deviceId);
                addFeedRule.setRepetitionPeriod(String.valueOf(week));
                addFeedRule.setStartTime(time);
                addFeedRule.setFeedType(typePosition == 1 ? 0 : 1);
                addFeedRule.setFeedNum(numberPosition + 1);
                addFeedRule.setFeedRule("2");
                addFeedRule.setEffectivTime("1");
                addFeedRule.setIsOpen(1);
                addFeedRule.setStartDate(date);

                petViewModel.addFeedRule(addFeedRule);
                snacksBottomSheetDialog.dismiss();
            });
            snacksBottomSheetDialog.show();
        });
    }

    public void checkDate(int w) {
        mBinding.smartRefreshLayout.finishRefresh();
        for (DateUtils.DateAndWeek datesAndWeek : datesAndWeeks) {
            if (datesAndWeek.getWeek() == w) {
                date = DateUtils.dateFormatDay1(datesAndWeek.getDay());
                this.week = datesAndWeek.getWeek();
                mBinding.tvProgress.setText(DateUtils.dateFormatDay3(datesAndWeek.getDay()) + "计划进度");
            }
        }
        mBinding.smartRefreshLayout.postDelayed(()->{
            mBinding.smartRefreshLayout.autoRefresh();
        },300);
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }

    @Override
    public void OnItemClick(View view, int position) {

    }

    @Override
    public void isOpen(boolean isOpen, int position) {
        FeedRule feedRule = timedPlans.get(position);
        loading();
        UpdateFeedRuleByDate updateFeedRuleByDate = new UpdateFeedRuleByDate();
        updateFeedRuleByDate.setDevice_time_task_id(feedRule.getId());
        updateFeedRuleByDate.setDate(date);
        updateFeedRuleByDate.setStatus(isOpen ? 1 : 0);
        petViewModel.updateFeedRuleByDate(updateFeedRuleByDate);
    }
}
